\documentclass[a4paper,12pt]{article}

\usepackage[margin=2cm]{geometry}
\usepackage{colortbl}
\usepackage{enumitem}
\usepackage{listings}

\setlength{\parskip}{1.5ex}
\setlength{\parindent}{0pt}

\lstset{basicstyle=\ttfamily, columns=flexible, framesep=0pt, framextopmargin=3pt,
    framexbottommargin=3pt, framexleftmargin=-1pt, frame=single, aboveskip=0.5cm,
    belowskip=0.2cm}

\title{C Experience}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.09.02 22:38}

\begin{document}

\maketitle

\section{Iterate through all possible sequences}

    Suppose now you want to iterate through all possible substrings of a string,
    say "hello", you want to get "h", "he", "hel", "hell", "hello", "e", "el"
    ...

    First, we know that a substring can be represented by a pair of numbers,
    with each denoting one end like {\em (i, j)}, which denotes a substring from
    {\em i} to {\em j}, and we also know that {\em i} and {\em j} can both vary
    from 0 to str\_len\_minus\_1 under the constrain that {\em i $\leq$ j} , then
    how does the iteration take place?

    We can think of all possible combinations of {\em i} and {\em j} this way,
    we sort the pairs first according to {\em i}, and then to {\em j}, which
    gives us a sequence like this:
    
    \begin{lstlisting}[gobble=3]
    (0, 0)
    (0, 1)
    ...
    (1, 1)
    (1, 2)
    ...
    (5, 5)
    ...
    \end{lstlisting}

    Now from the sequence above, we find the way to iterate through all possible
    pairs, which can be described in C like this:

    \begin{lstlisting}[gobble=3]
    for (i = 0; i < len; i++) {
            for (j = i; j < len; j++) {
                    ...
            }
    }
    \end{lstlisting}

    \textbf{CONCLUSION:}

    For any iteration, what you need to do is find some regularity of the
    underlying sequence, which is programmable.

\section{Take action once a second}

    Suppose you want to execute some code snippet once a second, then what are
    you going to do? Seems the most obvious solution is to use \verb=sleep=, but
    in fact, \verb=sleep= can only help you in some cases, instead of
    \verb=sleep=, one can use the following code to achieve this effect. (This a
    very brilliant idea that I learned from a post on linuxquestions.org)

    \begin{lstlisting}[gobble=3]
    tick_tick = time(NULL);

    while (...) {
            if (tick_tick != time(NULL)) {
                    tick_tick = time(NULL);
                    /* do something here */
            }
    }
    \end{lstlisting}

\section{A way to set a flag}

    Consider the following program which changes a flag within a single
    statement.

    \begin{lstlisting}
    flag = true;
    
    for (i = 0; i < len; i++) {
            /* the following achieves the same effect as:
             * if (ia[i] != 0)
             *         flag = false;
             * but it only occupies one line.
             */
            flag = flag && (ia[i] == 0);
    }
    
    flag = false;
    
    for (i = 0; i < len; i++) {
            flag = flag || (ia[i] == 0);
    }
    \end{lstlisting}

\section{Some Very Very Important Notes}

    \begin{enumerate}[leftmargin=3ex]
        \item Always be clear about how many states a variable could be in before you
            do any operation on it. (I got punished almost every time I forgot it)
    \end{enumerate}

\end{document}


